//==============================================================================================
/**
 * @file	factory_bmp.c
 * @brief	「バトルファクトリー」BMP
 * @author	Satoshi Nohara
 * @date	2007.03.15
 */
//==============================================================================================
#include "common.h"
#include "system/fontproc.h"
#include "system/pm_str.h"
#include "system/buflen.h"
#include "system/wordset.h"
#include "savedata/config.h"

#include "factory_sys.h"
#include "factory_bmp.h"


//==============================================================================================
//
//	定義
//
//==============================================================================================
//「トレーナー名」
#define WIN_TR1_PX			(1)
#define WIN_TR1_PY			(8)
#define WIN_TR1_SX			(8)
#define WIN_TR1_SY			(2)
#define WIN_TR1_CGX			(1)

//「パートナー名」
#define WIN_TR2_PX			(1)
#define WIN_TR2_PY			(1)
#define WIN_TR2_SX			(8)
#define WIN_TR2_SY			(2)
#define WIN_TR2_CGX			(WIN_TR1_CGX + WIN_TR1_SX * WIN_TR1_SY)

//「選択したポケモン名1」
#define WIN_POKE1_PX		(1)
#define WIN_POKE1_PY		(11)
#define WIN_POKE1_SX		(10)
#define WIN_POKE1_SY		(2)
#define WIN_POKE1_CGX		(WIN_TR2_CGX + WIN_TR2_SX * WIN_TR2_SY)
#define WIN_POKE1_MULTI_PX	(4)		// マルチ用Ｘ座標

//「選択したポケモン名2」
#define WIN_POKE2_PX		(11)
#define WIN_POKE2_PY		(11)
#define WIN_POKE2_SX		(10)
#define WIN_POKE2_SY		(2)
#define WIN_POKE2_CGX		(WIN_POKE1_CGX + WIN_POKE1_SX * WIN_POKE1_SY)
#define WIN_POKE2_MULTI_PX	(18)	// マルチ用Ｘ座標

//「選択したポケモン名3」
#define WIN_POKE3_PX		(21)
#define WIN_POKE3_PY		(11)
#define WIN_POKE3_SX		(10)
#define WIN_POKE3_SY		(2)
#define WIN_POKE3_CGX		(WIN_POKE2_CGX + WIN_POKE2_SX * WIN_POKE2_SY)

//「会話」
#define WIN_TALK_PX			( 2 )
#define WIN_TALK_PY			( 19 )
#define WIN_TALK_SX			( 27 )
#define WIN_TALK_SY			( 4 )
#define WIN_TALK_CGX		( 1 )

// 選択中のポケモン名（下画面）
#define WIN_POKEICON_NAME_PX	( 12 )
#define WIN_POKEICON_NAME_PY	( 1 )
#define WIN_POKEICON_NAME_SX	( 10 )
#define WIN_POKEICON_NAME_SY	( 2 )
#define WIN_POKEICON_NAME_CGX	( WIN_TALK_CGX + WIN_TALK_SX * WIN_TALK_SY )

//「もどる」
#define WIN_COMM_RET_PX		( 2 )
#define WIN_COMM_RET_PY		( 15 )
#define WIN_COMM_RET_SX		( 7 )
#define WIN_COMM_RET_SY		( 2 )
#define WIN_COMM_RET_CGX	( WIN_POKEICON_NAME_CGX + WIN_POKEICON_NAME_SX * WIN_POKEICON_NAME_SY )

//「ちゅうし」
#define WIN_COMM_END_PX		( 23 )
#define WIN_COMM_END_PY		( 15 )
#define WIN_COMM_END_SX		( 7 )
#define WIN_COMM_END_SY		( 2 )
#define WIN_COMM_END_CGX	( WIN_COMM_RET_CGX + WIN_COMM_RET_SX * WIN_COMM_RET_SY )

//「つよさをみる」
#define WIN_STATUS_PX		( 2 )
#define WIN_STATUS_PY		( 12 )
#define WIN_STATUS_SX		( 12 )
#define WIN_STATUS_SY		( 2 )
#define WIN_STATUS_CGX		( WIN_COMM_END_CGX + WIN_COMM_END_SX * WIN_COMM_END_SY )

//「レンタルする」
#define WIN_SELECT_PX		( 18 )
#define WIN_SELECT_PY		( 12 )
#define WIN_SELECT_SX		( WIN_STATUS_SX )
#define WIN_SELECT_SY		( WIN_STATUS_SY )
#define WIN_SELECT_CGX		( WIN_STATUS_CGX + WIN_STATUS_SX * WIN_STATUS_SY )

//「はずす」
#define WIN_OUT_PX			( 18 )
#define WIN_OUT_PY			( 12 )
#define WIN_OUT_SX			( WIN_STATUS_SX )
#define WIN_OUT_SY			( WIN_STATUS_SY )
#define WIN_OUT_CGX			( WIN_SELECT_CGX + WIN_SELECT_SX * WIN_SELECT_SY )

//「とじる」
#define WIN_CLOSE_PX		( 10 )
#define WIN_CLOSE_PY		( 15 )
#define WIN_CLOSE_SX		( WIN_STATUS_SX )
#define WIN_CLOSE_SY		( WIN_STATUS_SY )
#define WIN_CLOSE_CGX		( WIN_OUT_CGX + WIN_OUT_SX * WIN_OUT_SY )

//「てばなす」（返却画面）
#define WIN_SUB_PX			( 18 )
#define WIN_SUB_PY			( 12 )
#define WIN_SUB_SX			( WIN_STATUS_SX )
#define WIN_SUB_SY			( WIN_STATUS_SY )
#define WIN_SUB_CGX			( WIN_SELECT_CGX )

//「うけとる」（交換画面）
#define WIN_ADD_PX			( 2 )
#define WIN_ADD_PY			( 15 )
#define WIN_ADD_SX			( WIN_STATUS_SX )
#define WIN_ADD_SY			( WIN_STATUS_SY )
#define WIN_ADD_CGX			( WIN_OUT_CGX )

//「とじる」（交換画面）
#define WIN_CLOSE2_PX		( 18 )
#define WIN_CLOSE2_PY		( 15 )
#define WIN_CLOSE2_SX		( WIN_STATUS_SX )
#define WIN_CLOSE2_SY		( WIN_STATUS_SY )
#define WIN_CLOSE2_CGX		( WIN_CLOSE_CGX )

//「パートナーが選択したポケモン名1」
#define WIN_POKE4_PX		(4)
#define WIN_POKE4_PY		(4)
#define WIN_POKE4_SX		(10)
#define WIN_POKE4_SY		(2)
//#define WIN_POKE4_CGX		(WIN_SEL_CGX + WIN_SEL_SX * WIN_SEL_SY)
#define WIN_POKE4_CGX		(WIN_TALK_CGX + WIN_TALK_SX * WIN_TALK_SY)

//「パートナーが選択したポケモン名2」
#define WIN_POKE5_PX		(18)
#define WIN_POKE5_PY		(4)
#define WIN_POKE5_SX		(10)
#define WIN_POKE5_SY		(2)
#define WIN_POKE5_CGX		(WIN_POKE4_CGX + WIN_POKE4_SX * WIN_POKE4_SY)

//会話ウィンドウキャラ
#define	BF_TALKWIN_CGX_SIZE	( TALK_WIN_CGX_SIZ )
#define	BF_TALKWIN_CGX_NUM	( 1024 - BF_TALKWIN_CGX_SIZE )

// ボタンキャラ
#define	BF_BUTTON_CGX_SIZ	( 9 )
#define	BF_BUTTON_CGX_NUM	( BF_TALKWIN_CGX_NUM - BF_BUTTON_CGX_SIZ )


//==============================================================================================
//
//	データ
//
//==============================================================================================
static const BMPWIN_DAT FactoryBmpData[] =
{
	{	// 0:「トレーナー名」
		BF_FRAME_WIN, WIN_TR1_PX, WIN_TR1_PY,
		WIN_TR1_SX, WIN_TR1_SY, BF_FONT_PAL, WIN_TR1_CGX
	},
	{	// 1:「パートナー名」
		BF_FRAME_WIN, WIN_TR2_PX, WIN_TR2_PY,
		WIN_TR2_SX, WIN_TR2_SY, BF_FONT_PAL, WIN_TR2_CGX
	},
	{	// 2:「ポケモン名1」
		BF_FRAME_WIN, WIN_POKE1_PX, WIN_POKE1_PY,
		WIN_POKE1_SX, WIN_POKE1_SY, BF_FONT_PAL, WIN_POKE1_CGX
	},
	{	// 3:「ポケモン名2」
		BF_FRAME_WIN, WIN_POKE2_PX, WIN_POKE2_PY,
		WIN_POKE2_SX, WIN_POKE2_SY, BF_FONT_PAL, WIN_POKE2_CGX
	},
	{	// 4:「ポケモン名3」
		BF_FRAME_WIN, WIN_POKE3_PX, WIN_POKE3_PY,
		WIN_POKE3_SX, WIN_POKE3_SY, BF_FONT_PAL, WIN_POKE3_CGX
	},
	{	// 5:「パートナーのポケモン名1」
		BF_FRAME_WIN, WIN_POKE4_PX, WIN_POKE4_PY,
		WIN_POKE4_SX, WIN_POKE4_SY, BF_FONT_PAL, WIN_POKE4_CGX
	},
	{	// 6:「パートナーのポケモン名2」
		BF_FRAME_WIN, WIN_POKE5_PX, WIN_POKE5_PY,
		WIN_POKE5_SX, WIN_POKE5_SY, BF_FONT_PAL, WIN_POKE5_CGX
	},

	/////////////////////////////////////////////////////////////////////
	{	//「会話」
		BF_FRAME_STR_S, WIN_TALK_PX, WIN_TALK_PY,
		WIN_TALK_SX, WIN_TALK_SY, BF_MSGFONT_PAL, WIN_TALK_CGX
	},
	{	// 選択中のポケモン名（下画面）
		BF_FRAME_STR_S, WIN_POKEICON_NAME_PX, WIN_POKEICON_NAME_PY,
		WIN_POKEICON_NAME_SX, WIN_POKEICON_NAME_SY, BF_FONT_PAL, WIN_POKEICON_NAME_CGX
	},
	{	//「もどる」
		BF_FRAME_STR_S, WIN_COMM_RET_PX, WIN_COMM_RET_PY,
		WIN_COMM_RET_SX, WIN_COMM_RET_SY, BF_BUTTON_PAL, WIN_COMM_RET_CGX
	},
	{	//「ちゅうし」
		BF_FRAME_STR_S, WIN_COMM_END_PX, WIN_COMM_END_PY,
		WIN_COMM_END_SX, WIN_COMM_END_SY, BF_BUTTON_PAL, WIN_COMM_END_CGX
	},

	{	//「つよさをみる」
		BF_FRAME_STR_S, WIN_STATUS_PX, WIN_STATUS_PY,
		WIN_STATUS_SX, WIN_STATUS_SY, BF_BUTTON_PAL, WIN_STATUS_CGX
	},
	{	//「レンタルする」
		BF_FRAME_STR_S, WIN_SELECT_PX, WIN_SELECT_PY,
		WIN_SELECT_SX, WIN_SELECT_SY, BF_BUTTON_PAL, WIN_SELECT_CGX
	},
	{	//「はずす」
		BF_FRAME_STR_S, WIN_OUT_PX, WIN_OUT_PY,
		WIN_OUT_SX, WIN_OUT_SY, BF_BUTTON_PAL, WIN_OUT_CGX
	},
	{	//「とじる」
		BF_FRAME_STR_S, WIN_CLOSE_PX, WIN_CLOSE_PY,
		WIN_CLOSE_SX, WIN_CLOSE_SY, BF_BUTTON_PAL, WIN_CLOSE_CGX
	},
	{	//「てばなす」（返却画面）
		BF_FRAME_STR_S, WIN_SUB_PX, WIN_SUB_PY,
		WIN_SUB_SX, WIN_SUB_SY, BF_BUTTON_PAL, WIN_SUB_CGX
	},
	{	//「うけとる」（交換画面）
		BF_FRAME_STR_S, WIN_ADD_PX, WIN_ADD_PY,
		WIN_ADD_SX, WIN_ADD_SY, BF_BUTTON_PAL, WIN_ADD_CGX
	},
	{	//「とじる」（交換画面）
		BF_FRAME_STR_S, WIN_CLOSE2_PX, WIN_CLOSE2_PY,
		WIN_CLOSE2_SX, WIN_CLOSE2_SY, BF_BUTTON_PAL, WIN_CLOSE2_CGX
	},
};


//==============================================================================================
//
//	プロトタイプ宣言
//
//==============================================================================================
void FactoryAddBmpWin( GF_BGL_INI* bgl, GF_BGL_BMPWIN* win );
void FactoryExitBmpWin( GF_BGL_BMPWIN* win );
void FactoryOffBmpWin( GF_BGL_BMPWIN* win );
void FactoryTalkWinPut( GF_BGL_BMPWIN * win, WINTYPE wintype );


//==============================================================================================
//
//	関数
//
//==============================================================================================

//--------------------------------------------------------------
/**
 * @brief	ビットマップ追加
 *
 * @param	ini		BGLデータ
 * @param	win		ビットマップウィンドウ
 *
 * @return	none
 */
//--------------------------------------------------------------
void FactoryAddBmpWin( GF_BGL_INI* bgl, GF_BGL_BMPWIN* win )
{
	u8 i;
	const BMPWIN_DAT* dat = FactoryBmpData;

	//ビットマップ追加
	for( i=0; i < FACTORY_BMPWIN_MAX; i++ ){
		GF_BGL_BmpWinAddEx( bgl, &win[i], &dat[i] );
		GF_BGL_BmpWinDataFill( &win[i], FBMP_COL_NULL );		//塗りつぶし
	}

	return;
}

//--------------------------------------------------------------
/**
 * @brief	ビットマップウィンドウ破棄
 *
 * @param	win		ビットマップウィンドウ
 *
 * @return	none
 */
//--------------------------------------------------------------
void FactoryExitBmpWin( GF_BGL_BMPWIN* win )
{
	u16	i;

	for( i=0; i < FACTORY_BMPWIN_MAX; i++ ){
		GF_BGL_BmpWinDel( &win[i] );
	}

	return;
}

//--------------------------------------------------------------
/**
 * @brief	ビットマップウィンドウオフ
 *
 * @param	win		ビットマップウィンドウ
 *
 * @return	none
 */
//--------------------------------------------------------------
void FactoryOffBmpWin( GF_BGL_BMPWIN* win )
{
	u16	i;

	for( i=0; i < FACTORY_BMPWIN_MAX; i++ ){
		GF_BGL_BmpWinOff( &win[i] );
	}

	return;
}

//--------------------------------------------------------------------------------------------
/**
 * @brief	会話ウィンドウ表示
 *
 * @param	win		BMPデータ
 * @param	wintype	ウィンドウタイプ
 *
 * @return	none
 */
//--------------------------------------------------------------------------------------------
void FactoryTalkWinPut( GF_BGL_BMPWIN * win, WINTYPE wintype )
{
	TalkWinGraphicSet( win->ini, GF_BGL_BmpWinGet_Frame(win), BF_TALKWIN_CGX_NUM,
						BF_TALKWIN_PAL, wintype, HEAPID_FACTORY );

	GF_BGL_BmpWinDataFill( win, 15 );
	BmpTalkWinWrite( win, WINDOW_TRANS_OFF, BF_TALKWIN_CGX_NUM, BF_TALKWIN_PAL );
	return;
}



//--------------------------------------------------------------------------------------------
/**
 * @brief	文字列表示（バッファ指定）
 *
 * @param	win		BMPWIN
 * @param	str		文字列バッファ
 * @param	x		表示基準Ｘ座標
 * @param	y		表示基準Ｙ座標
 * @param	fnt		フォント
 * @param	col		カラー
 * @param	mode	表示モード
 *
 * @return	none
 *
 * @li	mode = FACTORYBMP_PRINT_MODE_LEFT   : 左詰
 * @li	mode = FACTORYBMP_PRINT_MODE_RIGHT  : 右詰
 * @li	mode = FACTORYBMP_PRINT_MODE_CENTER : 中央
 */
//--------------------------------------------------------------------------------------------
void FACTORYBMP_StrPrintCore(
		GF_BGL_BMPWIN * win, STRBUF * str,
		u32 x, u32 y, FONT_TYPE fnt, GF_PRINTCOLOR col, u32 mode )
{
	if( mode == FACTORYBMP_PRINT_MODE_RIGHT ){
		x -= FontProc_GetPrintStrWidth( fnt, str, 0 );
	}else if( mode == FACTORYBMP_PRINT_MODE_CENTER ){
		x -= ( FontProc_GetPrintStrWidth( fnt, str, 0 ) / 2 );
	}
	GF_STR_PrintColor( win, fnt, str, x, y, MSG_NO_PUT, col, NULL );
}

//--------------------------------------------------------------------------------------------
/**
 * @brief	文字列表示（固定文字列）
 *
 * @param	win		BMPWIN
 * @param	man		メッセージマネージャ
 * @param	msgID	文字列ＩＤ
 * @param	x		表示基準Ｘ座標
 * @param	y		表示基準Ｙ座標
 * @param	fnt		フォント
 * @param	col		カラー
 * @param	mode	表示モード
 *
 * @return	none
 *
 * @li	mode = FACTORYBMP_PRINT_MODE_LEFT   : 左詰
 * @li	mode = FACTORYBMP_PRINT_MODE_RIGHT  : 右詰
 * @li	mode = FACTORYBMP_PRINT_MODE_CENTER : 中央
 */
//--------------------------------------------------------------------------------------------
void FACTORYBMP_StrPrint(
		GF_BGL_BMPWIN * win, MSGDATA_MANAGER * man,
		u32 msgID, u32 x, u32 y, FONT_TYPE fnt, GF_PRINTCOLOR col, u32 mode )
{
	STRBUF * str = MSGMAN_AllocString( man, msgID );

	FACTORYBMP_StrPrintCore( win, str, x, y, fnt, col, mode );

	STRBUF_Delete( str );
}

//--------------------------------------------------------------------------------------------
/**
 * @brief	マルチの場合、上画面のポケモン表示位置を変更する
 *
 * @param	win		BMPWIN
 *
 * @return	none
 */
//--------------------------------------------------------------------------------------------
void FACTORYBMP_MultiPosChange( GF_BGL_BMPWIN * win )
{
	GF_BGL_BmpWinSet_PosX( &win[0], WIN_POKE1_MULTI_PX );
	GF_BGL_BmpWinSet_PosX( &win[1], WIN_POKE2_MULTI_PX );
}


#include "../graphic/frontier_bg_def.h"

//--------------------------------------------------------------------------------------------
/**
 * @brief	ボタンＢＧ読み込み
 *
 * @param	bgl		GF_BGL_INI
 * @param	ah		アーカイブハンドル
 *
 * @return	none
 */
//--------------------------------------------------------------------------------------------
void FACTORYBMP_ButtonLoad( GF_BGL_INI * bgl, ARCHANDLE * ah )
{
	ArcUtil_HDL_BgCharSet(
		ah, BF_BUTTON_NCGR_BIN, bgl, BF_FRAME_STR_S, BF_BUTTON_CGX_NUM, 0, 1, HEAPID_FACTORY );
	ArcUtil_HDL_PalSet(
		ah, BF_BUTTON_NCLR, PALTYPE_SUB_BG, BF_BUTTON_PAL*0x20, 0x40, HEAPID_FACTORY );
}

//--------------------------------------------------------------------------------------------
/**
 * @brief	ボタン表示
 *
 * @param	bgl		GF_BGL_INI
 * @param	ah		アーカイブハンドル
 * @param	num		ボタン番号
 *
 * @return	none
 */
//--------------------------------------------------------------------------------------------
void FACTORYBMP_ButtonOn( GF_BGL_INI * bgl, ARCHANDLE * ah, u32 num )
{
	NNSG2dScreenData * scrn;
	void * buf;
	u16 * raw;
	u8	py, sy;
	u8	i, j;

	buf = ArcUtil_HDL_ScrnDataGet( ah, BF_BUTTON01_NSCR_BIN+num, 1, &scrn, HEAPID_FACTORY );

	if( num == 0 ){
		py = 11;
		sy = 7;
	}else{
		py = 14;
		sy = 4;
	}

	raw = (u16 *)scrn->rawData;

	for( i=0; i<sy; i++ ){
		for( j=0; j<32; j++ ){
			GF_BGL_ScrFill(
				bgl, BF_FRAME_STR_S, raw[i*32+j]+BF_BUTTON_CGX_NUM,
				j, py+i, 1, 1, BF_BUTTON_PAL );
		}
	}

	sys_FreeMemoryEz( buf );
}

//--------------------------------------------------------------------------------------------
/**
 * @brief	ボタン非表示
 *
 * @param	bgl		GF_BGL_INI
 * @param	num		ボタン番号
 *
 * @return	none
 */
//--------------------------------------------------------------------------------------------
void FACTORYBMP_ButtonOff( GF_BGL_INI * bgl, u32 num )
{
	u16	py, sy;

	if( num == 0 ){
		py = 11;
		sy = 7;
	}else{
		py = 14;
		sy = 4;
	}
	GF_BGL_ScrFill( bgl, BF_FRAME_STR_S, 0, 0, py, 32, sy, BF_BUTTON_PAL );
	GF_BGL_LoadScreenV_Req( bgl, BF_FRAME_STR_S );
}